package com.itluma.myshopping.service.impl;

import com.itluma.myshopping.dao.ProductDAO;
import com.itluma.myshopping.dao.impl.ProductDAOImpl;
import com.itluma.myshopping.entity.Product;
import com.itluma.myshopping.pojo.PageBean;
import com.itluma.myshopping.service.ProductService;
import com.itluma.myshopping.servlet.ProductServlet;

import java.util.List;

/**
 * @program: Ourshop6team
 * @author: 小陆
 * @create: 2021-03-15 19:54
 **/

public class ProductServiceImpl implements ProductService {

    @Override
    public PageBean<Product> showAll(Integer tid, Integer page, Integer pageSize) {
        ProductDAO productDAO = new ProductDAOImpl();
        List<Product> products = productDAO.showAll(tid, page, pageSize);
        PageBean<Product> productBList = new PageBean<>();
        productBList.setDate(products);
        productBList.setCurrentPage(page);
        productBList.setPageSize(pageSize);
        //productBList.setKeyWorkName(keyword);
        Integer count = productDAO.countByTid(Integer.valueOf(tid));
        productBList.setTotalCount(count);
        if (page == 1) {
            productBList.setFirstPage(true);
        }
        Integer totalPage = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
        productBList.setTotalPage(totalPage);
        if (totalPage == page) {
            productBList.setLastPage(true);
        }

//        return pageBeanProduct;
        return productBList;
    }

    public static void main(String[] args) {
        ProductService productServlet = new ProductServiceImpl();
        PageBean<Product> productPageBean = productServlet.showAll(2, 13, 2);
        System.out.println(productPageBean);
    }

}
